package br.home.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.home.entitys.Garcom;
import br.home.entitys.Mesa;
import br.home.jdbc.ConnectionFactory;

/**
 * @author : Efraim Gentil - Fortaleza/CE
 * @project: app-barraca
 * @packege: br.home.servlets
 * @file   : MesaServlet.java
 * @date   : 19/10/2011 at 11:27:12
 */
/**
 * Servlet implementation class MesaServlet
 */

public class MesaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MesaServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession mySession = request.getSession();

		if (mySession.getAttribute("ac") != null) {
			if (mySession.getAttribute("ac").equals("editarpedidos")) {
				try {
					Connection con = ConnectionFactory.getConnection();
				} catch (Exception e) {
					
				}
				request.setAttribute("mesa", mySession.getAttribute("mesa"));
				RequestDispatcher rd = request
						.getRequestDispatcher("WEB-INF/jsp/mesas.jsp");
				rd.forward(request, response);
			}
		} else {

			if (request.getParameter("ac") != null
					&& request.getParameter("ac").equals("editarpedido")) {
				try {
					int idmesa = Integer.parseInt(request.getParameter("mesa"));
					if (idmesa > 0) {
						try {
							Connection con = ConnectionFactory.getConnection();
							String sql = "SELECT * FROM mesa WHERE idmesa = ? ";
							PreparedStatement stmt = con.prepareStatement(sql);
							stmt.setInt(1, idmesa);
							ResultSet result = stmt.executeQuery();
							boolean existe = false;
							while (result.next()) {
								existe = true;
								Mesa mesa = new Mesa();
								mesa.setIdmesa(result.getInt("idmesa"));
								mesa.setNumeromesa(result.getInt("numeromesa"));
								mesa.setDataatendimento(result
										.getDate("dataatendimento"));
								mesa.setHorainicio(result.getTime("horainicio"));
								mesa.setHorafim(result.getTime("horafim"));
								mesa.setValorpago(result.getDouble("valorpago"));
								mesa.setValor10porcento(result
										.getDouble("valor10porcento"));
								mesa.setFinalizada(result
										.getBoolean("finalizada"));
								mesa.setIdgarcom(result.getInt("idgarcom"));
								Garcom garcom = new Garcom();
								String sql2 = "SELECT * FROM garcom WHERE idgarcom = ? LIMIT 1";
								PreparedStatement stmt2 = con
										.prepareStatement(sql2);
								stmt2.setInt(1, mesa.getIdgarcom());
								ResultSet result2 = stmt2.executeQuery();
								while (result2.next()) {
									garcom.setId(result2.getInt("idgarcom"));
									garcom.setNome(result2.getString("nome"));
									garcom.setTelefone(result2
											.getString("telefone"));
									garcom.setAtivo(result2.getBoolean("ativo"));
								}
								result2.close();
								stmt2.close();
								mesa.setGarcom(garcom);
								mySession.setAttribute("mesa", mesa);
								mySession.setAttribute("ac", "editarpedidos");
							}
							result.close();
							stmt.close();
							con.close();
							if (!existe) {
								response.sendRedirect("home?returnerro=notfound");
							} else {
								response.sendRedirect("mesa");
							}
						} catch (Exception e) {
							// e.printStackTrace();
							response.sendRedirect("home?returnerro=notfound");
						}
					} else {
						response.sendRedirect("home");
					}
				} catch (NumberFormatException e) {
					response.sendRedirect("home?returnerro=notfound");
				}
			}
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
